﻿package mortal.game.view.CollectExchange.cellRender
{
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.CollectExchange.other.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.mvc.core.*;

    public class CollectExchangeCellRender extends GCellRenderer
    {
        private var _getBtn:GLoadingButton;
        private var _recipeData:CollectExchangeDataGroup;
        private var _leftTimesLabel:GTextFiled;
        private var _exchangeTakeArrow:SWFPlayer;
        private var _itemList:GTileList;
        private var _CellBg:GBitmap;

        public function CollectExchangeCellRender()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.bg(1, 0, 688, 90, this, ImagesConst.collectExchangeCellBg));
            this._getBtn = UIFactory.gLoadingButton(ResFileConst.CollectExchangeBtn, 505 + 57, 32 - 10, 101, 41, this);
            this._leftTimesLabel = UIFactory.gTextField("", 505 + 50, 70 - 10, 250, 20, this, GlobalStyle.textFormatBai, true);
            this._leftTimesLabel.mouseEnabled = false;
            this._exchangeTakeArrow = UICompomentPool.getUICompoment(SWFPlayer);
            this._itemList = UIFactory.tileList(0, 30, 270, 70, this);
            this._itemList.rowHeight = 57;
            this._itemList.columnWidth = 45;
            this._itemList.horizontalGap = 20;
            this._itemList.verticalGap = 0;
            this._itemList.columnCount = 12;
            this._itemList.rowCount = 1;
            this._itemList.setStyle("cellRenderer", CollectExchangeItemCellRender);
            this._itemList.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._itemList.direction = ScrollBarDirection.HORIZONTAL;
            this._getBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onMoveBtnMouseHandler);
            LoaderHelp.addResCallBack(ResFileConst.collectExchange, this.onResLoadCompl);
            Dispatcher.addEventListener(EventName.CollectExchangeOutputFlyToBackPack, this.onFlyToBackPack);
            return;
        }// end function

        private function onResLoadCompl() : void
        {
            if (isDisposed)
            {
                return;
            }
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            this._recipeData = param1 as CollectExchangeDataGroup;
            var _loc_2:* = this._itemList.columnWidth;
            var _loc_3:* = this._itemList.horizontalGap;
            var _loc_4:* = this.getListX(_loc_2, _loc_3);
            this._itemList.x = _loc_4;
            var _loc_5:* = this._recipeData.dataItemList.length;
            var _loc_6:* = this._recipeData.needNum;
            var _loc_7:* = this._recipeData.leftTimes;
            var _loc_8:* = this._recipeData.maxTimes;
            var _loc_9:* = this._itemList.x + 42;
            var _loc_10:* = this._itemList.y + 10;
            var _loc_11:int = 65;
            var _loc_12:int = 0;
            this._leftTimesLabel.htmlText = Language.getStringByParam(90001, _loc_7, _loc_8);
            if (this._recipeData)
            {
                this._itemList.dataProvider = this.getDataProvider();
                this._itemList.drawNow();
            }
            EffectManager.glowFilterUnReg(this._getBtn);
            this.removeArrow();
            if (!this._recipeData.isSatisfyRecipe())
            {
                this._getBtn.mouseEnabled = false;
                this._getBtn.filterEnabled = false;
            }
            else
            {
                this._getBtn.visible = true;
                this._getBtn.mouseEnabled = true;
                EffectManager.glowFilterReg(this._getBtn);
                this.addArrow();
            }
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_2:int = 0;
            var _loc_5:Object = null;
            var _loc_1:* = new DataProvider();
            var _loc_3:* = this._recipeData.itemInfos;
            var _loc_4:* = this._recipeData.needNum;
            _loc_2 = 0;
            while (_loc_2 < _loc_3.length)
            {
                
                _loc_5 = _loc_3[_loc_2].extData;
                _loc_5["signType"] = 1;
                if (_loc_2 == (_loc_4 - 1))
                {
                    _loc_5["signType"] = 2;
                }
                else if (_loc_2 == (_loc_3.length - 1))
                {
                    _loc_5["signType"] = 3;
                }
                _loc_5["exchangeType"] = 1;
                if (_loc_2 >= _loc_4)
                {
                    _loc_5["exchangeType"] = 2;
                }
                _loc_1.addItem(_loc_3[_loc_2]);
                _loc_2++;
            }
            return _loc_1;
        }// end function

        private function getListX(param1:int, param2:int) : int
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_3:* = this._recipeData.itemNum;
            if (_loc_3 > 0)
            {
                _loc_4 = 540;
                _loc_5 = _loc_3 * (param1 + param2) - param2;
                return (_loc_4 - _loc_5) / 2;
            }
            return 0;
        }// end function

        private function onMoveBtnMouseHandler(event:MouseEvent) : void
        {
            var _loc_2:* = this._recipeData.id;
            Dispatcher.dispatchEvent(new DataEvent(EventName.CollectExchangeGetOutput, _loc_2));
            return;
        }// end function

        private function addArrow() : void
        {
            this._exchangeTakeArrow.timeRate = 2;
            this._exchangeTakeArrow.load("guideArrow.swf", ModelType.NormalSwf, null);
            this._exchangeTakeArrow.move(this._getBtn.x - 10, this._getBtn.y + 20);
            this.addChild(this._exchangeTakeArrow);
            return;
        }// end function

        private function removeArrow() : void
        {
            DisplayUtil.removeMe(this._exchangeTakeArrow);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._getBtn.dispose(param1);
            this._leftTimesLabel.dispose(param1);
            this._itemList.dispose(param1);
            this.removeArrow();
            this._exchangeTakeArrow.dispose(param1);
            this._getBtn = null;
            this._leftTimesLabel = null;
            this._itemList = null;
            LoaderHelp.removeResEvent(ResFileConst.collectExchange, this.onResLoadCompl);
            return;
        }// end function

        private function onFlyToBackPack(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Object = null;
            var _loc_7:CollectExchangeItemCellRender = null;
            var _loc_8:Point = null;
            if (isDisposed)
            {
                return;
            }
            var _loc_2:* = event.data as int;
            if (_loc_2 == this._recipeData.id)
            {
                _loc_3 = this._itemList.length;
                _loc_4 = this._recipeData.needNum;
                _loc_5 = _loc_4;
                while (_loc_5 < _loc_3)
                {
                    
                    _loc_6 = this._itemList.getItemAt(_loc_5);
                    _loc_7 = this._itemList.itemToCellRenderer(_loc_6) as CollectExchangeItemCellRender;
                    if (_loc_7)
                    {
                        _loc_8 = _loc_7._item.localToGlobal(new Point(0, 0));
                        FlyToNavbarTool.flyToBackPack(_loc_7._item.bitmapdata, _loc_8);
                    }
                    _loc_5++;
                }
            }
            return;
        }// end function

    }
}
